import unittest
from toolclass.random_number import random_number
from toolclass.time_samp import TimeStamp
import requests
from conf.readConfig import ReadConfig
from lib.md5_hash import Md5Hash

timestamp = TimeStamp()


class KamiOrderNumber():
    def setUp(self):
        print("****开始执行****")

    def tearDown(self):
        print("****执行结束****")

    def kami_order_number(self):
        timesamp = timestamp.time10()
        # 生成签名()
        data = {
            "merchantId":"22988",
            "notifyUrl":"http://test.recharge.1688sup.cn/record.php",
            "number":"1",
            "outTradeNo":"yanwei"+str(random_number()),
            "productId":"107",
            "timeStamp":str(timesamp),
            "key":"3e7bc3da64191001a5d7f5053e7ab"
        }
        param = '&'.join([k + '=' + v for k, v in data.items()])
        # print(param)
        sign = Md5Hash().md5_hash(param).upper()
        # print(sign)

        #接口下单
        payload = {"merchantId": data["merchantId"], "outTradeNo": data["outTradeNo"], "productId": data["productId"],"timeStamp": data["timeStamp"],"number": data["number"],"notifyUrl": data["notifyUrl"] ,"sign": sign}
        # print(payload)
        headers = {
            "Content-Type": "application/x-www-form-urlencoded"
        }

        url = "http://test.openapi.1688sup.cn/card/order"
        r = requests.post(url=url,headers=headers,data=payload)
        resp = r.text
        return data["outTradeNo"]


if __name__ == "__main__":
    KamiOrderNumber().kami_order_number()
